<template>
  <div>
    <c-title :hide="false" :text="'企业管理'"></c-title>
    <van-field v-model="detail.name" label="企业名称" :border="false" placeholder="请输入企业名称" input-align="right" />
    <div class="logo">
      <div class="logo-left">logo</div>
      <div class="logo-right">
        <yz_uploader v-model="logo" :max_count="1" ref="yz_uploader"></yz_uploader>
      </div>
    </div>
    <div class="btn">
      <van-button color="#daa032" @click="setdata">提交</van-button>
    </div>
  </div>
</template>

<script>
import yz_uploader from "components/ui_components/yz_uploader";
export default {
  components: { yz_uploader },
  data() {
    return {
      detail:{},
      isAdd:true,
      logo: []
    };
  },
  activated(){
    this.initdata();
  },
  methods: {
    initdata(){
      this.detail = {};
      this.isAdd = true;
      this.logo = [];
      if(this.$route.name == "enterpriseEdit"){
        this.getdata(this.$route.params.id);
        this.isAdd = false;
      }
    },
    getdata(id){
      $http.post("plugin.lawyer-platform.frontend.controllers.business.getDetail",{id},"正在获取数据中...").then(({result,msg,data})=>{
        if(result){
          this.detail = data.detail || {};
          this.logo[0] = this.detail.logo_img;
          this.$refs.yz_uploader.fileList1 = this.logo;
        }else this.$toast(msg);
      })
    },
    setdata(){
      const toast = this.$toast.loading({
        duration: 0,
        forbidClick: true,
        message: `正在${this.isAdd?"添加":"修改"}中...`,
        selector: '#custom-selector',
      });
      let json = {form:{name:this.detail.name,logo:this.logo[0]}};
      let url = "plugin.lawyer-platform.frontend.controllers.business.create";
      if(!this.isAdd){
        url = "plugin.lawyer-platform.frontend.controllers.business.edit";
        json.id = this.$route.params.id;
      }
      if(this.$route.query.share){
        json.form.merchant_member_id =this.$route.query.merchant_member_id
      }
      if(localStorage.getItem("uid")==null){
         this.$toast.clear();
      }
      $http.post(url,json).then(({result,msg,data})=>{
        this.$toast.clear();
        if(result){
          this.$toast({
            type: 'success',
            message: msg,
          })
          if(this.$route.query.share){
            this.$router.push(this.fun.getUrl("lawyerPlatform"))
          }else{
            this.$router.go(-1)

          }
        }else  this.$toast(msg);
      })
    },
    onRead_backend(e) {
      let fd = new FormData();
      fd.append("file", e.file);
      this.logo.status = "uploading";
      this.logo.message = "上传中...";
      axios
        .post(this.fun.getRealUrl("upload.uploadPic"), fd, {
          headers: {
            "Contnent-Type": "multipart/form-data"
          }
        })
        .then(res => {
          if (res.data.result == 1) {
            this.logo.status = "done";
            this.$toast(res.data.msg);
            this.logo.url = res.data.data.img_url;
          } else {
            this.logo.message = "failed";
            this.$toast(res.data.msg);
          }
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.logo {
  display: flex;
  background-color: #fff;
  align-items: center;
  padding: 1rem;
  justify-content: space-between;

  ::v-deep .van-icon {
    // padding: 0.5rem;
    // border: 1px solid #ccc;
  }
}

.btn {
  position: fixed;
  width: 100vw;
  bottom: 0;
  background-color: #fff;
  box-shadow: 0 -2px 7px 0 rgba(213, 213, 213, 0.59);
  padding: 0.5714rem 1.7857rem;

  ::v-deep .van-button--normal {
    width: 100%;
    border-radius: 0.3571rem;
  }
}
</style>
